package com.laozhao.EasySales.view.master;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableRowSorter;

import com.laozhao.EasySales.business.MasterServer;
import com.laozhao.EasySales.control.master.SysSqlAction;
import com.laozhao.EasySales.model.entity.SysSql;
import com.laozhao.EasySales.view.AbstractPanel;
import com.laozhao.dreamframe.util.FrameFactory;
import com.laozhao.dreamframe.view.DreamTable;
import com.laozhao.dreamframe.view.DreamTableModel;

public class SysSqlPanel extends AbstractPanel<SysSql>  {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8242147959976563882L;
	// private JPanel searchPanel;
	private JScrollPane tablePane;
	private DreamTable table;
	private DreamTableModel<SysSql> model;

	// private TableRowSorter<DreamTableModel> sorter;

	public SysSqlPanel() {
		initCompants();
		initEvent();
	}

	
	public void init() {
		initData();
	}
	
	private void initEvent() {
		addButton.setActionCommand("Add");
		modifyButton.setActionCommand("Modify");
		deleteButton.setActionCommand("Delete");
//		searchButton.setActionCommand("Search");
		table.addMouseListener((SysSqlAction)FrameFactory.getInstance(SysSqlAction.class));
		addButton.addActionListener((SysSqlAction)FrameFactory.getInstance(SysSqlAction.class));
		modifyButton.addActionListener((SysSqlAction)FrameFactory.getInstance(SysSqlAction.class));
		deleteButton.addActionListener((SysSqlAction)FrameFactory.getInstance(SysSqlAction.class));
//		searchButton.addActionListener((SysSqlAction)FrameFactory.getInstance(SysSqlAction.class));
		deleteButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				SysSql sql = new SysSql();
				sql.setSqlId("hello");
				sql.setSqlValue("hello!!!!");
				sql.setDescription("");
				model.addRow(sql);
				
			}
		});
	}


	private void initCompants() {
		setLayout(new BorderLayout());
		// create search pane
		add(getControlPanel(), BorderLayout.NORTH);
		// Create a table with a sorter.
		model = new DreamTableModel<SysSql>(SysSql.class);

		TableRowSorter<DreamTableModel<SysSql>> sorter = new TableRowSorter<DreamTableModel<SysSql>>(model);
		table = new DreamTable(model);
		table.setRowSorter(sorter);
		table.setPreferredScrollableViewportSize(new Dimension(500, 70));
		table.setFillsViewportHeight(true);

		// For the purposes of this example, better to have a single
		// selection.
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tablePane = new JScrollPane(table);

		this.add(tablePane, BorderLayout.CENTER);
	}

	private void initData() {
		MasterServer mServer = (MasterServer) FrameFactory
				.getInstance(MasterServer.class);
		List<SysSql> list = mServer.searchSqls(null,null);
		model.setModeList(list);

	}
	

	@Override
	public SysSql getDataModel() {
		int selIndex = table.getSelectedRow();
		dataModel = (SysSql) model.getRowModelData(selIndex);
		return dataModel;
	}


	@Override
	public String getTitle() {
		panelTitle = "SysSql";
		return panelTitle;
	}

	
}